package com.dishdex.menuhub.app.controller.category;

import com.dishdex.menuhub.app.domain.category.CategoryLevelOneVO;
import com.dishdex.menuhub.app.domain.category.CategoryListVO;
import com.dishdex.menuhub.app.domain.category.CategorySubsequentLevelVO;
import com.dishdex.menuhub.app.domain.category.CategoryWithDishesVO;
import com.dishdex.menuhub.app.domain.dish.DishItemVO;
import com.dishdex.menuhub.app.util.CategoryConverter;
import com.dishdex.menuhub.app.util.DishConverter;
import com.dishdex.menuhub.module.module.category.entity.Category;
import com.dishdex.menuhub.module.module.dish.entity.Dish;
import com.dishdex.menuhub.module.module.category.service.CategoryService;
import com.dishdex.menuhub.module.module.dish.service.DishService;
import com.dishdex.menuhub.module.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 用于展示一级分类和二级分类
     */
    @RequestMapping("/category/list")
    public Response categoryList(@RequestParam(value = "categoryId", required = false) Long categoryId) {
        List<Category> categoryList;
        try {
            categoryList = categoryService.getAll(categoryId);
        } catch (RuntimeException e) {
            CategoryListVO categoryListVO = new CategoryListVO();
            categoryListVO.setMessage(e.getMessage());
            return new Response(4004, categoryListVO);
        }

        // 存储一级分类列表
        List<CategoryLevelOneVO> categoryLevelOneVOList = new ArrayList<>();

        // 存储一级分类映射
        Map<Long, CategoryLevelOneVO> levelOneCategoryMap = new HashMap<>();

        //获取一级分类Id
        List<Long> levelOneCategoryIds = categoryService.getLevelOneIds();

        /*
         * 先将一级分类进行处理，即先寻找一级分类，并将其转化为VO，存储到列表中和Map中；
         * */
        for (Category category : categoryList) {
            if (levelOneCategoryIds.contains(category.getId())) {
                //找到一级分类后，将其转换为VO，并进行存储
                CategoryLevelOneVO categoryLevelOneVO = CategoryConverter.toCategoryLevelOneVO(category);
                categoryLevelOneVOList.add(categoryLevelOneVO);
                levelOneCategoryMap.put(category.getId(), categoryLevelOneVO);
            }
        }

        /*
         * 寻找二级分类，并将其转换为VO，存储到一级分类中的children列表中去
         * */

        //获取二级分类Id
        List<Long> categoryLevelTwoVOList = categoryService.getLevelTwoIds();

        for (Category category : categoryList) {
            if (categoryLevelTwoVOList.contains(category.getId())) {
                //找到二级分类后，从levelOneCategoryMap取出其对应的一级分类，并将找到的二级分类添加到children列表中
                CategoryLevelOneVO categoryLevelOneVO = levelOneCategoryMap.get(category.getParentId());
                if (categoryLevelOneVO != null) {
                    categoryLevelOneVO.getChildren().add(CategoryConverter.toCategoryLevelTwoVO(category));
                }
            }
        }

        CategoryListVO categoryListVO = new CategoryListVO();
        categoryListVO.setList(categoryLevelOneVOList);
        return new Response(1001, categoryListVO);
    }

    /**
     * 用于展示三级分类和具体的菜品
     * subsequentId参数用于接受二级分类的Id；
     * 根据传进来的二级分类Id，生成其下的next级分类列表
     */
    @Autowired
    private DishService dishService;

    @RequestMapping("/category/subsequent/list")
    public Response categorySubsequentList(@RequestParam(value = "subsequentId") Long subsequentId,
                                           @RequestParam(value = "nextId", required = false) Long nextId) {
        List<Category> categoryList;
        try {
            categoryList = categoryService.getMore(subsequentId);
        } catch (RuntimeException e) {
            CategoryWithDishesVO categoryWithDishesVO = new CategoryWithDishesVO();
            categoryWithDishesVO.setMessage(e.getMessage());
            return new Response(4004, categoryWithDishesVO);
        }
        List<CategorySubsequentLevelVO> categorySubsequentLevelVOList = new ArrayList<>();
        for (Category category : categoryList) {
            categorySubsequentLevelVOList.add(CategoryConverter.toCategoryLevelThreeVO(category));
        }

        CategoryWithDishesVO categoryWithDishesVO = new CategoryWithDishesVO();
        categoryWithDishesVO.setCategoryList(categorySubsequentLevelVOList);

        //当nextId不存在时，默认采用subsequent列表中的第一个nextLevel分类的Id
        CategorySubsequentLevelVO categorySubsequentLevelVO = categorySubsequentLevelVOList.get(0);

        Long categoryId;
        if (nextId == null) {
            categoryId = categorySubsequentLevelVO.getId();
        } else {
            categoryId = nextId;
        }

        List<Dish> dishList = dishService.getByCategoryId(categoryId);
        List<DishItemVO> dishItemVOList = new ArrayList<>();
        for (Dish dish : dishList) {
            dishItemVOList.add(DishConverter.toDishItemVO(dish, categoryService));
        }

        categoryWithDishesVO.setDishList(dishItemVOList);
        return new Response(1001, categoryWithDishesVO);

    }

}